<?php

/**
 * Vehicle form base class.
 *
 * @method Vehicle getObject() Returns the current form's model object
 *
 * @package    symfony
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseVehicleForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                   => new sfWidgetFormInputHidden(),
      'make_id'              => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Make'), 'add_empty' => false)),
      'model_id'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Model'), 'add_empty' => false)),
      'series'               => new sfWidgetFormInputText(),
      'body_style_id'        => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('BodyStyle'), 'add_empty' => false)),
      'year'                 => new sfWidgetFormInputText(),
      'transmission'         => new sfWidgetFormInputText(),
      'engine'               => new sfWidgetFormInputText(),
      'fuel'                 => new sfWidgetFormInputText(),
      'exterior_color'       => new sfWidgetFormInputText(),
      'doors'                => new sfWidgetFormInputText(),
      'cylinders'            => new sfWidgetFormInputText(),
      'mileage'              => new sfWidgetFormInputText(),
      'stereo_description'   => new sfWidgetFormInputText(),
      'description'          => new sfWidgetFormTextarea(),
      'stock_id'             => new sfWidgetFormInputText(),
      'price'                => new sfWidgetFormInputText(),
      'view'                 => new sfWidgetFormInputText(),
      'is_featured'          => new sfWidgetFormInputCheckbox(),
      'is_special'           => new sfWidgetFormInputCheckbox(),
      'wof_expires'          => new sfWidgetFormInputText(),
      'registration_expires' => new sfWidgetFormInputText(),
      'plate'                => new sfWidgetFormInputText(),
      'created_at'           => new sfWidgetFormDateTime(),
      'updated_at'           => new sfWidgetFormDateTime(),
      'features_list'        => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Feature')),
    ));

    $this->setValidators(array(
      'id'                   => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'make_id'              => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Make'))),
      'model_id'             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Model'))),
      'series'               => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'body_style_id'        => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('BodyStyle'))),
      'year'                 => new sfValidatorInteger(),
      'transmission'         => new sfValidatorInteger(),
      'engine'               => new sfValidatorInteger(),
      'fuel'                 => new sfValidatorInteger(),
      'exterior_color'       => new sfValidatorString(array('max_length' => 255)),
      'doors'                => new sfValidatorInteger(),
      'cylinders'            => new sfValidatorInteger(),
      'mileage'              => new sfValidatorInteger(),
      'stereo_description'   => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'description'          => new sfValidatorString(),
      'stock_id'             => new sfValidatorInteger(),
      'price'                => new sfValidatorInteger(),
      'view'                 => new sfValidatorInteger(array('required' => false)),
      'is_featured'          => new sfValidatorBoolean(array('required' => false)),
      'is_special'           => new sfValidatorBoolean(array('required' => false)),
      'wof_expires'          => new sfValidatorString(array('max_length' => 255)),
      'registration_expires' => new sfValidatorString(array('max_length' => 255)),
      'plate'                => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'created_at'           => new sfValidatorDateTime(),
      'updated_at'           => new sfValidatorDateTime(),
      'features_list'        => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Feature', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('vehicle[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Vehicle';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['features_list']))
    {
      $this->setDefault('features_list', $this->object->Features->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveFeaturesList($con);

    parent::doSave($con);
  }

  public function saveFeaturesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['features_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Features->getPrimaryKeys();
    $values = $this->getValue('features_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Features', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Features', array_values($link));
    }
  }

}
